import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:news_flutter/models/video_model.dart';
import 'package:news_flutter/widgets/custom/cache_image_widget.dart';
import 'package:video_player/video_player.dart';

class VideoDetailPage extends StatefulWidget {
  final VideoModel videoModel;

  const VideoDetailPage({Key key, this.videoModel}) : super(key: key);

  @override
  _VideoDetailPageState createState() => _VideoDetailPageState();
}

class _VideoDetailPageState extends State<VideoDetailPage> {
  VideoPlayerController _controller;
  bool _showCover =true;
  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.videoModel.playaddr)
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
    _controller.setLooping(true);
  }
  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light,
      child: Scaffold(
        body: SafeArea(
          child: _body(),
        ),
        backgroundColor: Colors.black,
      ),
    );
  }

  Widget _body() {
    return GestureDetector(
      onTap: (){
        //切换播放暂停
        _controller.value.isPlaying
            ? _controller.pause()
            : _controller.play();
        if (_showCover ==true){
          _showCover =false;
        }
        setState(() {
        });
      },
      onHorizontalDragEnd: (detail){
        Navigator.pop(context);

      },
      onVerticalDragEnd: (detail){
        Navigator.pop(context);
      },

      child: Stack(
        children: <Widget>[
          //视频播放
          Container(
            color: Colors.black,
            child: Center(
              child: _controller.value.initialized
                  ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              ):Container(),
            )
          ),
          //封面
          _showCover?Container(
            width: double.infinity,
            height: double.infinity,
            child: MyCacheImage(
              url: widget.videoModel.coverurl,
              fit: BoxFit.fitWidth,
            ),
          ):Container(),
          //播放Icon
          Container(
            child: Center(
              child: _controller.value.isPlaying?Container():Icon(Icons.play_circle_filled,size: 88,color: Colors.white,),
            ),
          ),
          //退出
          Positioned(
            right: 0,top: 0,
            child: IconButton(
              onPressed: (){
                Navigator.pop(context);
              },
              icon: Icon(Icons.close,color: Colors.white,),
            ),
          ),
        ],
      ),
    );
  }
}
